<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="98px">
      <el-form-item label="报销类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择报销类型" clearable>
          <el-option v-for="dict in dict.type.reimburse_type" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <!--<el-form-item label="报销人" prop="userId">
        <el-input v-model="queryParams.userId" placeholder="请输入报销人"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>-->
      <el-form-item label="申请人姓名" prop="userName">
        <el-input v-model="queryParams.userName" placeholder="请输入报销申请人姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!--<el-form-item label="请款人人姓名" prop="userName" v-if="queryParams.status != '0'">
        <el-input v-model="queryParams.qkrmc" placeholder="请输入请款人姓名"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>-->
      <el-form-item label="请款事由" prop="qksy" v-if="queryParams.status != '0'">
        <el-input v-model="queryParams.qksy" placeholder="请输入请款事由" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="资金用途" prop="zjyt" v-if="queryParams.status != '0'">
        <el-input v-model="queryParams.zjyt" placeholder="请输入资金用途" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!--<el-form-item label="报销日期" prop="imburseDate">
        <el-date-picker clearable v-model="queryParams.imburseDate"
                        type="date" value-format="yyyy-MM-dd" placeholder="请选择报销日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="发票金额" prop="taxMoney">
        <el-input v-model="queryParams.taxMoney" placeholder="请输入发票金额"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>-->
      <!--<el-form-item label="报销状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择报销状态" clearable>
          <el-option v-for="dict in dict.type.imburse_status"
                     :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!--<el-col :span="1.5" v-if="addBtn == '1'">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAddNewImbursement('1')"
                   v-hasPermi="['develop:GoodsReimbursement:zc']">自采报销
        </el-button>
      </el-col>
      <el-col :span="1.5" v-if="addBtn == '1'">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAddNewImbursement('0')"
                   v-hasPermi="['develop:GoodsReimbursement:gc']">公采打款
        </el-button>
      </el-col>-->
      <el-col :span="1.5" v-if="addBtn == '1'">
        <!--<el-button type="primary" plain icon="el-icon-plus" size="mini" @click="addNewreimbursementForFee('2')"
          v-hasPermi="['develop:GoodsReimbursement:fy']">费用报销
        </el-button>-->
      </el-col>
      <!--<el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single"
                   @click="handleUpdate" v-hasPermi="['develop:GoodsReimbursement:edit']">修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-popconfirm title="确认删除选择的数据项？" @confirm="handleDelete({})">
          <el-button slot="reference" type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
                     v-hasPermi="['develop:GoodsReimbursement:remove']">删除
          </el-button>
        </el-popconfirm>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini"
                   @click="handleExport" v-hasPermi="['develop:GoodsReimbursement:export']">导出
        </el-button>
      </el-col>-->
      <!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
    </el-row>

    <el-table border v-loading="loading" :data="dataList" @selection-change="handleSelectionChange"
      :max-height="tableMaxHeight">
      <el-table-column label="报销状态" align="center" prop="status" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.imburse_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="报销类型" align="center" prop="type" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.reimburse_type" :value="scope.row.type" />
        </template>
      </el-table-column>
      <!--<el-table-column label="报销人" align="center" prop="userId"/>-->
      <el-table-column label="申请人姓名" align="center" prop="userName"  />
      <el-table-column label="请款人人姓名" align="center" prop="qkrmc"  />
      <el-table-column label="报销申请日期" align="center" prop="imburseDate" width="180" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.imburseDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="报销金额" align="center" prop="imburseMoney"  />
      <el-table-column label="剩余发放金额" align="center" prop="remRemain"  />
      <!--<el-table-column label="关联发票" align="center" prop="taxId"/>
      <el-table-column label="发票号码" align="center" prop="taxNo"/>
      <el-table-column label="发票金额" align="center" prop="taxMoney"/>-->
      <el-table-column label="请款事由" align="center" prop="qksy" show-overflow-tooltip />
      <el-table-column label="资金用途" align="center" prop="zjyt" show-overflow-tooltip />
      <!--      <el-table-column label="报销描述" align="center" prop="infomation"/>-->
      <!--<el-table-column label="关联商品id" align="center" prop="goodsIds"/>
      <el-table-column label="附件" align="center" prop="file"/>
              <el-table-column label="报销状态" align="center" prop="status">
                  <template slot-scope="scope">
                          <dict-tag :options="dict.type.imburse_status" :value="scope.row.status"/>
                  </template>
              </el-table-column>
      <el-table-column label="备注" align="center" prop="remark"/>-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleView(scope.row)">
            查看详情
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-view" v-if="queryParams.status == '0' && addBtn == '0'"
            @click="handleCheckSend(scope.row, '1')">审核
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-view" v-if="queryParams.status == '1'"
            @click="handleCheckSend(scope.row, '2')">发放
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-edit"
            v-if="(scope.row.status == '0' || scope.row.status == '-1') && addBtn == '1'"
            @click="handleUpdate(scope.row)" v-hasPermi="['develop:GoodsReimbursement:edit']">修改
          </el-button>
          <el-popconfirm :title="`确认删除当前数据项？`" @confirm="handleDelete(scope.row)" style="margin-left: 10px;">
            <el-button slot="reference" size="mini" type="text" icon="el-icon-delete"
              v-if="scope.row.status == '0' && addBtn == '1'">删除
            </el-button>
          </el-popconfirm>
          <el-button type="text" size="mini" icon="el-icon-download" @click="exportExcel(scope.row)">
            导出
          </el-button>

          <el-button type="text" size="mini" icon="el-icon-download" @click="exportFksqd(scope.row)">
            付款申请单
          </el-button>
          <!--<el-button type="warning" plain icon="el-icon-download" size="mini"
                     @click="handleExport" v-hasPermi="['develop:GoodsOrderIn:export']">导出
          </el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!--新增申请-->
    <goods-reimbursement-form-for-order ref="formForOrder" @ok="getList"></goods-reimbursement-form-for-order>
    <!--状态修改--审核发放-->
    <goods-reimbursement-form-for-view ref="formForOrderView" @ok="getList"></goods-reimbursement-form-for-view>
    <!--详情-->
    <!--    <goods-reimbursement-form ref="goodsReimbursementForm"></goods-reimbursement-form>-->
    <!--费用报销-->
    <goods-reimbursement-form-for-fee ref="goodsReimbursementFormForFee"
      @ok="getList"></goods-reimbursement-form-for-fee>

  </div>
</template>

<script>
  import {
    deleteAction,
    downloadFile,
    getAction
  } from '@/api/manage'
  import GoodsReimbursementFormForOrder
  from '@/views/develop/goodsReimbursement/module/GoodsReimbursementFormForOrder'
  import GoodsReimbursementFormForView from "./module/GoodsReimbursementFormForView";
  // import GoodsReimbursementForm from "./module/GoodsReimbursementForm";
  import GoodsReimbursementFormForFee from "./module/GoodsReimbursementFormForFee";

  export default {
    name: "GoodsReimbursementList",
    components: {
      GoodsReimbursementFormForOrder,
      GoodsReimbursementFormForView,
      // GoodsReimbursementForm,
      GoodsReimbursementFormForFee,
    },
    dicts: ['reimburse_type', 'imburse_status'],
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 报销发票信息管理表格数据
        dataList: [],
        // 表格的高度
        tableMaxHeight: window.innerHeight - 320,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 20,
          type: null,
          qksy: null,
          zjyt: null,
          userId: null,
          userName: null,
          imburseDate: null,
          taxId: null,
          taxNo: null,
          taxMoney: null,
          infomation: null,
          goodsIds: null,
          file: null,
          status: null,
          create_by: this.$store.getters.name,
        },
        url: {
          list: '/goodsReimbursement/goodsReimbursement/list',
          remove: '/goodsReimbursement/goodsReimbursement/',
          export: '/goodsReimbursement/goodsReimbursement/export',
        },
        addBtn: "0"
      };
    },
    created() {
      this.initQuery();
      this.getList();
    },
    methods: {
      exportFksqd(id) {
        let that = this;
        that.loading = false;
        that.$confirm('是否确认导出付款申请单?', "确认", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          downloadFile('/goodsReimbursementDetail/goodsReimbursementDetail/exportFksqd', id);
        });
      },

      // 获取打印机列表
      getPrinterList() {
        if (window.hiwebSocket.opened === false) {
          this.$notify.error('打印机客户端未连接')
        }
        // 模板对象获取
        const printerList_ = this.hiprintTemplate.getPrinterList()
        console.info(printerList_)
      },

      // 使用 hiPrintPlugin 控件打印
      /*confirmPrintPrint(row) {
          // 如果在 main.js 中设置了取消自动连接客户端 是获取不到打印机列表的！！！
          if (window.hiwebSocket.opened === false) {
              this.$notify.error('打印机客户端未连接，请点击右上角头像下载打印客户端')
          }
          // 这一句代码 如果打印出来有问题 可以尝试加进去，没有出现 则不用加
          // 初始化 provider
          this.$pluginName.init({
              providers: [defaultElementTypeProvider()]
          })
          // 这一句代码 如果打印出来有问题 可以尝试加进去，没有出现 则不用加
          this.$pluginName.PrintElementTypeManager.buildByHtml(this.$('.ep-draggable-item'))
          // 清空原内容
          this.$('#printDivXm3').empty()
          // 不使用全局对象, 使用模版打印，在线设计模版：https://ccsimple.gitee.io/vue-plugin-hiprint/
          const hiprintTemplate = new this.$pluginName.PrintTemplate({
              template:
                  {
                      "panels": [{
                          "index": 0,
                          "name": 1,
                          "height": 297,
                          "width": 210,
                          "paperHeader": 49.5,
                          "paperFooter": 780,
                          "printElements": [{
                              "options": {
                                  "left": 17.5,
                                  "top": 87.5,
                                  "height": 72,
                                  "width": 550,
                                  "field": "dayincanshu",
                                  "groupFooterFormatter": "function(t,e){return\"这里自定义统计脚信息\"}",
                                  "groupFieldsFormatter": "function(type,options,data){ return [\"name\"] }",
                                  "coordinateSync": false,
                                  "widthHeightSync": false,
                                  "columns": [[{
                                      "width": 70,
                                      "title": "行号",
                                      "field": "id",
                                      "checked": true,
                                      "columnId": "id",
                                      "fixed": false,
                                      "rowspan": 2,
                                      "colspan": 1
                                  }, {
                                      "width": 100,
                                      "title": "人员信息",
                                      "checked": true,
                                      "fixed": false,
                                      "rowspan": 1,
                                      "colspan": 2
                                  }, {
                                      "width": 100,
                                      "title": "销售统计",
                                      "checked": true,
                                      "fixed": false,
                                      "rowspan": 1,
                                      "colspan": 2
                                  }], [{
                                      "width": 120,
                                      "title": "姓名",
                                      "field": "name",
                                      "checked": true,
                                      "columnId": "name",
                                      "fixed": false,
                                      "rowspan": 1,
                                      "colspan": 1,
                                      "align": "left"
                                  }, {
                                      "width": 120,
                                      "title": "性别",
                                      "field": "gender",
                                      "checked": true,
                                      "columnId": "gender",
                                      "fixed": false,
                                      "rowspan": 1,
                                      "colspan": 1
                                  }, {
                                      "width": 120,
                                      "title": "销售数量",
                                      "field": "count",
                                      "checked": true,
                                      "columnId": "count",
                                      "fixed": false,
                                      "rowspan": 1,
                                      "colspan": 1
                                  }, {
                                      "width": 120,
                                      "title": "销售金额",
                                      "field": "amount",
                                      "checked": true,
                                      "columnId": "amount",
                                      "fixed": false,
                                      "rowspan": 1,
                                      "colspan": 1
                                  }]]
                              },
                              "printElementType": {
                                  "title": "表格",
                                  "type": "table",
                                  "editable": true,
                                  "columnDisplayEditable": true,
                                  "columnDisplayIndexEditable": true,
                                  "columnTitleEditable": true,
                                  "columnResizable": true,
                                  "columnAlignEditable": true,
                                  "isEnableEditField": true,
                                  "isEnableContextMenu": true,
                                  "isEnableInsertRow": true,
                                  "isEnableDeleteRow": true,
                                  "isEnableInsertColumn": true,
                                  "isEnableDeleteColumn": true,
                                  "isEnableMergeCell": true
                              }
                          }],
                          "paperNumberLeft": 565.5,
                          "paperNumberTop": 819,
                          "paperNumberContinue": true,
                          "watermarkOptions": {
                              "content": "vue-plugin-hiprint",
                              "rotate": 25,
                              "timestamp": true,
                              "format": "YYYY-MM-DD HH:mm"
                          }
                      }]
                  }
          })
          // 挂载打印内容
          hiprintTemplate.design('#printDivXm3')
          // 打印数据，要和 上面 panel 内的 field 参数一致
          const table = [{
              id: '我是条形码内容',
              name: '我是测试内容1',
              gender: '我是测试内容2'
          },
              {
                  id: '我是条形码内容',
                  name: '我是测试内容1',
                  gender: '我是测试内容2'
              },]

          const contentToPrint = document.getElementById("contentToPrint");
          var dayincanshu = {
              // dayincanshu:this.dayincanshu
              dayincanshu: table
          }
          hiprintTemplate.print(dayincanshu)
      },*/

      exportExcel(id) {
        let that = this;
        that.loading = false;
        that.$confirm('是否确认导出报销申请信息表?', "确认", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          downloadFile('/goodsReimbursementDetail/goodsReimbursementDetail/exportExcel', id);
        });
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        if (row.type != '2') {
          this.$refs.formForOrder.handleUpdate(row);
        } else {
          this.$refs.goodsReimbursementFormForFee.handleUpdate(row);
        }

      },
      handleCheckSend(row, status) {
        this.$refs.formForOrderView.handleCheckSend(row, status);
      },
      handleView(row) {
        this.$refs.formForOrderView.handleUpdate(row, false);
        // this.$refs.goodsReimbursementForm.handleUpdate(row);
      },
      handleAddNewImbursement(type) {
        this.$refs.formForOrder.handleAddNewImbursement(type);
      },
      addNewreimbursementForFee(type) {
        this.$refs.goodsReimbursementFormForFee.addNewreimbursementForFee(type);
      },
      initQuery() {
        let that = this;
        if (that.$route.query.status) {
          that.queryParams.status = that.$route.query.status;
        }
        if (that.$route.query.addBtn) {
          that.addBtn = that.$route.query.addBtn;
        }
        //申请人只能看到自己的申请单
        if (that.addBtn == '1') {
          that.queryParams.createBy = that.$store.getters.name
        }
      },
      /** 查询报销发票信息管理列表 */
      getList() {
        this.loading = true;
        getAction(this.url.list, this.queryParams).then(response => {
          this.dataList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.$refs.form.handleAdd();
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        deleteAction(this.url.remove, ids).then(response => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download(this.url.export, {
          ...this.queryParams
        }, `goodsReimbursement_${new Date().getTime()}.xlsx`)
      },
    }
  };
</script>
